package com.tianda.his.modules.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianda.his.modules.system.dto.DictDataDto;
import com.tianda.his.modules.system.entity.SysDictData;
import com.tianda.his.modules.system.entity.SysDictType;
import com.tianda.his.modules.system.mapper.DictTypeMapper;
import com.tianda.his.modules.system.service.SysDictDataService;
import com.tianda.his.modules.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* @author jinken
* @description 针对表【sys_dict_data(字典数据表)】的数据库操作Service实现
* @createDate 2025-09-12 15:04:23
*/
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData>
    implements SysDictDataService{

    @Autowired
    private RedisTemplate  redisTemplate;

    @Autowired
    private DictTypeMapper dictTypeMapper;

    @Override
    public Page<SysDictData> listForPage(DictDataDto dictDataDto) {

        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictData::getDictType,dictDataDto.getDictType());
        queryWrapper.like(StrUtil.isNotBlank(dictDataDto.getDictLabel()),SysDictData::getDictLabel,dictDataDto.getDictLabel());
        queryWrapper.like(StrUtil.isNotBlank(dictDataDto.getStatus()),SysDictData::getStatus,dictDataDto.getStatus());

        Page<SysDictData> page = new Page<>(dictDataDto.getPageNum(), dictDataDto.getPageSize());

        this.page(page,queryWrapper);

        return  page;
    }

    @Override
    @Transactional
    public void addDictData(DictDataDto dictDataDto) {
        //将dto转换成 SysDictData
        SysDictData sysDictData = new SysDictData();


    }

    @Override
    public List<SysDictData> getDictDataByType(String dictType) {
        //先从redis查询
        List<SysDictData> dictDataList = (List<SysDictData>) redisTemplate.boundHashOps("dictCache").get(dictType);
        if(CollUtil.isNotEmpty(dictDataList)){
            System.out.println("============redis查==============");
            return dictDataList;
        }
        System.out.println("============从数据库查查==============");
        //从数据库查询
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictData::getDictType, dictType);
        return this.list(queryWrapper);
    }
}




